package com.dongdong.service.impl;

import com.dongdong.mapper.TbSpecificationMapper;
import com.dongdong.model.TbBrand;
import com.dongdong.model.TbSpecification;
import com.dongdong.model.TbSpecificationExample;
import com.dongdong.page.PageParams;
import com.dongdong.page.PageResult;
import com.dongdong.service.SpecOptionService;
import com.dongdong.service.SpecService;
import com.dongdong.vo.BrandVO;
import com.dongdong.vo.SpecOptionVO;
import com.dongdong.vo.SpecVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    private TbSpecificationMapper specificationMapper;

    @Autowired
    private SpecOptionService optionService;

    @Override
    public PageParams<SpecVO> getSpecList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TbSpecification> list = specificationMapper.selectByExample(null);
        PageInfo<TbSpecification> pageInfo = new PageInfo<>(list);
        List<SpecVO> collect = list.stream().map(speclist -> {
            SpecVO vo = new SpecVO();
            BeanUtils.copyProperties(speclist, vo);
            return vo;
        }).collect(Collectors.toList());
        return new PageParams<>(pageInfo.getPageNum(),pageInfo.getPageSize(),collect,pageInfo.getTotal(),pageInfo.getPages());
    }


    //    @Override
//    public PageTWOTWO<SpecVO> getSpecList(PageParam<SpecVO> pageParam,SpecVO specVO) {
//        TbSpecificationExample example=new TbSpecificationExample();
//        //判断name不为空
//        if (!StrUtil.isEmpty(pageParam.getParams().getSpecName())){
//            example.createCriteria().andSpecNameLike("%"+pageParam.getParams().getSpecName()+"%");
//        }
//        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
//        //模糊查询之后进行查询
//        List<TbSpecification> speclist = specificationMapper.selectByExample(example);
//        PageInfo<TbSpecification> pageInfo=new PageInfo<>(speclist);
//        //转换成vo
//        List<SpecVO> collect = speclist.stream().map(speclist1 -> {
//            SpecVO specVO = new SpecVO();
//            BeanUtils.copyProperties(speclist1, specVO);
//            return specVO;
//        }).collect(Collectors.toList());
//
//        return new PageResult(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), collect);
//    }


    @Override
    public List<SpecVO> findSpecList(SpecVO specVO) {
        TbSpecificationExample example=new TbSpecificationExample();
        if (specVO.getSpecName() != null){
            example.createCriteria().andSpecNameLike("%"+specVO.getSpecName()+"%");
        }
        List<TbSpecification> speclist = specificationMapper.selectByExample(example);
        List<SpecVO> collect = speclist.stream().map(spec -> {
            SpecVO vo = new SpecVO();
            BeanUtils.copyProperties(spec, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public SpecVO getSpecById(Long id) {
        //查询规格管理表
        TbSpecification spec = specificationMapper.selectByPrimaryKey(id);
        if (spec == null){
            return null;
        }
        //转换
        SpecVO vo = new SpecVO();
        BeanUtils.copyProperties(spec,vo);

        //调用option的service，通过sepcid去查询option
        List<SpecOptionVO> optionVOList = optionService.getSpecOptionBySpecId(spec.getId());
        vo.setSpecOptionVOList(optionVOList);
        return vo;
    }

    @Override
    @Transactional   //事务控制    事务是将多条sql捆绑在一起，要么全部执行  要么全部失败
    public int updateSecp(SpecVO specVO) {
        //1.根据规格id更新规格表的数据
        TbSpecification specification=new TbSpecification();
        BeanUtils.copyProperties(specVO,specification);
        specificationMapper.updateByPrimaryKeySelective(specification);
        //2.根据规格id删除规格表中明细的数据
        int i = optionService.deleteBySpecId(specVO.getId());
            //3.把list集合中的数据添加进规格明细表
        int i1 = optionService.insertSpecOption(specVO);
        if (i > 0 && i1 >0){
            return 1;//两条成功返回1
        }
        return 0;//失败返回0
    }

    @Override
    public int insertSpec(SpecVO specVO) {
        //1.增加规格表   增加完成之后返回一个specId
        TbSpecification spec = new TbSpecification();
        BeanUtils.copyProperties(specVO,spec);
        int i1 = specificationMapper.insert(spec);
        //2.增加规格明细表
        Long id = spec.getId();
        List<SpecOptionVO> specOptionVOList = specVO.getSpecOptionVOList();
        for (SpecOptionVO optionVO : specOptionVOList) {
            optionVO.setSpecId(id);
        }
        int i = optionService.insertSpecOption(specVO);
        if (i > 0 && i1 > 0){
            return i;
        }
        return 0;
    }

    @Override
    public int deleteSpec(Long[] arr) {
        //1.删除规格表
        TbSpecificationExample example = new TbSpecificationExample();
        example.createCriteria().andIdIn(Arrays.asList(arr));
        int i = specificationMapper.deleteByExample(example);
        //2.删除规格明细表
        int i1=0;
        for (Long specId : arr) {
            i1 = optionService.deleteBySpecId(specId);
        }
        if (i > 0 && i1 >0){
            return i;
        }
        return 0;
    }


    //获取select2
    @Override
    public List<SpecVO> getSpecListAndSelect2() {
        List<TbSpecification> tbSpecifications = specificationMapper.selectByExample(null);
        List<SpecVO> collect = tbSpecifications.stream().map(speclist -> {
            SpecVO vo = new SpecVO();
            BeanUtils.copyProperties(speclist, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

}
